package tmi.li.txclck;


import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import tmi.li.txclck.R;

public class TxClckConfiguration extends Activity {
	
	static final String TAG = "tmi.li.txclck.TxClckConfiguration";
	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	String format = "12h";
	String hex = "";
	String bhex = "";
	private String appname= "";
	
	
	public TxClckConfiguration() {
		super();
	}
	

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setResult(RESULT_CANCELED);
		setContentView(R.layout.configuration);
		
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		// If they gave us an intent without the widget id, just bail.
		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}
		
		//registering event listener
		findViewById(R.id.selectapp).setOnClickListener(mAppClickListener);
		findViewById(R.id.save).setOnClickListener(mSaveOnClickListener);
		final RadioButton h12 = (RadioButton) findViewById(R.id.h12);
		final RadioButton h24 = (RadioButton) findViewById(R.id.h24);
		h12.setOnClickListener(radio_listener);
		h24.setOnClickListener(radio_listener);
		
	}
	
	
	
	//menu dialog for picking a app
	public void openAppMenu() {
		//IntentCategorys
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);;
		//Init Lists
		@SuppressWarnings("rawtypes")
		final List AppList = getPackageManager().queryIntentActivities(mainIntent, 0);
		//final CharSequence[] appPackageNames = new CharSequence[AppList.size()];
		final CharSequence[] appNames = new CharSequence[AppList.size()];
		final ResolveInfo[] appInfos =  new ResolveInfo[AppList.size()];
		int i = 0;
		//Get appnames and packagenames
		for (Object item:  getPackageManager().queryIntentActivities( mainIntent, 0)) {
			appInfos[i] = (ResolveInfo)item;
			appNames[i] = getPackageManager().getApplicationLabel(appInfos[i].activityInfo.applicationInfo);
			i++;
		}

		//Create dialog.builder
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Pick a app");
		builder.setItems(appNames, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				Toast.makeText(getApplicationContext(), "selected App: "+appNames[item], Toast.LENGTH_SHORT).show();	
				appname = appInfos[item].activityInfo.packageName.toString()+"/"+appInfos[item].activityInfo.name.toString();
				TextView text = (TextView)findViewById(R.id.appname);
				text.setText(appNames[item]);
			}
		});

		//Create dialog with builder
		AlertDialog alert = builder.create();

		//Display dialog
		alert.show();
	}
	
	//onclick listener for AppSelector DIalog
	View.OnClickListener mAppClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			//final Context context = AppWidgetConfigure.this;
			
			//saving the city name in the var city & starting the thread 
			openAppMenu();
		}
	};
	
	//onClick listener for the Save button
	View.OnClickListener mSaveOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			
			final Context context = TxClckConfiguration.this;
			
			//getting the values for Font Color and Background Color
			EditText text = (EditText)findViewById(R.id.color);
			hex = text.getText().toString();
			text = (EditText)findViewById(R.id.bcolor);
			bhex = text.getText().toString();
			
			
			try { 
				// tries to set the FontColor and Backgroundcolor on a dummy TextView
				// if this fails the user used a wrong color code
				TextView colortest = (TextView)findViewById(R.id.TextView09);
				colortest.setTextColor(Color.parseColor(hex));
				colortest.setTextColor(Color.parseColor(bhex));
				
				if(appname == "")  throw new InterruptedException();
				
				//return the Result
				Intent resultValue = new Intent();
				resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
				setResult(RESULT_OK, resultValue);
				
				//saving the id of the widget and the preferences
				TxPref txpref = new TxPref(TxPref.PREFS_NAME_TXCLCK);
				txpref.savePrefs(context, mAppWidgetId, ""+bhex+";"+hex+";"+format);
				txpref.saveId(context, mAppWidgetId);
				txpref.saveAppName(context, mAppWidgetId, appname);
				
				//updating the widget
				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
				TxClckProvider.updateWidget(context, appWidgetManager, mAppWidgetId);
				
				finish();
				
			}catch(InterruptedException a) {
				Toast.makeText(getApplicationContext(), "no App selected !!", Toast.LENGTH_LONG).show();
			}catch(Exception e) {
				//User used wrong Color code; 
				Toast.makeText(getApplicationContext(), "Wrong Color code !!", Toast.LENGTH_LONG).show();
			}
		}
	};
	
	
	
	//onclick listener for the radio buttons
	View.OnClickListener radio_listener = new View.OnClickListener() {
		public void onClick(View v) {
			// Perform action on clicks
			RadioButton rb = (RadioButton) v;
			if(rb.getText().toString().contains("4")){        	
				format="24h";
			}else {
				format="12h";
			}
		}
	};
	
	


	

}
